/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once
#include <math.h>
#include <cmath>
#include <float.h>

#include "Vector3.h"
#include "Random.h"
#include "../Hogshead.Common.Functions.h"

#ifdef max
#undef max
#endif

#ifdef min
#undef min
#endif

namespace Hogshead
{
	namespace Common
	{
		class Math
		{
		public:

			static void initialize();
			static void finalize();

			const static float Pi;
			const static float Epsilon;

			

			static inline float sqrt(float in_f)
			{
				return std::sqrtf(in_f);
			}

			static inline double sqrt(double in_d)
			{
				return std::sqrt(in_d);
			}

			static inline float abs(float in_f)
			{
				return std::abs(in_f);
			}

			//trig
			static inline float cos(float in_theta)
			{
				return std::cos(in_theta);
			}
	
			static inline float sin(float in_theta)
			{
				float result = std::sin(in_theta);
				if(_isnan(result) || !_finite(result))
					error("float point error");

				return result;
			}
			static inline float tan(float in_theta)
			{
				return std::tan(in_theta);
			}

			static inline float cot(float in_theta)
			{
				return 1.0f / std::tan(in_theta);
			}

			static inline float acos(float in_value)
			{
				if(in_value > 1.0f)
					in_value = 1.0f;
				else if( in_value < -1.0f )
					in_value = -1.0f;

				float result = std::acos(in_value);
				if(_isnan(result) || !_finite(result))
					error("float point error");

				return result;
			}

			static inline float asin(float in_value)
			{
				return std::asin(in_value);
			}

			static inline float atan2(float in_y, float in_x)
			{
				return std::atan2(in_y, in_x);
			}

			static inline float log(float x)
			{
				return std::log(x);
			}


			static float clamp(float min, float max, float value);
			static int clamp(int min, int max, int value);

			static float degreesToRadians(float in_theta);
			/** 
				Solves equations of the form: ax^2 + bx + c = 0 
				returns the  number of solutions
			**/
			
			static int solveQuadratic(float in_a, float in_b, float in_c, float& out_x1, float& out_x2);

			static inline float mean(const Vector3& v)
			{
				return (v.x + v.y + v.z) / 3.0f;
			}

			static float lerp(float in_start, float in_end, float t);

			static inline float hermite(float start, float end, float t)
			{
				return lerp(start, end, t*t*(3.0f-(2.0f*t)));
			}


			static float exp(float x)
			{
				return std::exp(x);
			}

			template<typename T>
			static inline T max(T a, T b)
			{
				return a > b ? a : b;
			};

			template<typename T>
			static inline T min(T a, T b)
			{
				return a < b ? a : b;
			};

			static inline float pow(float in, int power)
			{
				return std::pow(in, power);
			}

			static inline float pow(float in, float power)
			{
				return std::powf(in, power);
			}

			static inline float sign(float a)
			{
				if(a == 0.0f)
					return 0.0f;
				return a < 0.0f ? -1.0f : 1.0f;
			}

			static bool equal(float a, float b, float error, bool errorAbsolute=false);

			static Random& random();
		private:
			static Random** _randoms;
		};
	}
}